<template>
  <Form ref="fm" :validation-schema="rules" v-slot="{ errors }" class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field
          v-model="form.account"
          name="account"
          class="input"
          type="text"
          placeholder="请输入用户名"
        />
      </div>
      <div class="error" v-show="errors.account">{{ errors.account }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field
          v-model="form.mobile"
          name="mobile"
          class="input"
          type="text"
          placeholder="请输入手机号"
        />
      </div>
      <div class="error" v-show="errors.mobile">{{ errors.mobile }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field
          v-model="form.code"
          name="code"
          class="input"
          type="text"
          placeholder="请输入验证码"
        />
        <span @click="handlerCode" class="code">{{  count === 0 ? "发送验证码" : `${count}秒后发送`}}</span>
      </div>
      <div class="error" v-show="errors.code">{{ errors.code }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          v-model="form.password"
          name="password"
          class="input"
          type="password"
          placeholder="请输入密码"
        />
      </div>
      <div class="error" v-show="errors.password">{{ errors.password }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field
          v-model="form.rePassword"
          name="rePassword"
          class="input"
          type="password"
          placeholder="请确认密码"
        />
      </div>
      <div class="error" v-show="errors.rePassword">
        {{ errors.rePassword }}
      </div>
    </div>
    <a href="javascript:;" @click="submit" class="submit">立即提交</a>
  </Form>
</template>

<script>
import { reactive } from 'vue'
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
// import { useCountDown } from '@/hooks/index'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { message } from '@/components/Message/index.js'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
// 导入发送验证码公共方法
import { useSendCode } from '@/hooks/page'

// 表单校验
const useValidateForm = () => {
  // 表单数据对象
  const form = reactive({
    account: null,
    mobile: null,
    code: null,
    password: null,
    rePassword: null
  })
  // 校验规则
  const rules = {
    account: schema.accountApi,
    mobile: schema.mobile,
    code: schema.code,
    password: schema.password,
    rePassword: schema.rePassword
  }

  return { form, rules }
}

// 注册账号并且绑定qq
const useSubmit = (fm, props, formData) => {
  const store = useStore()
  const router = useRouter()
  // 点击注册账号的事件回调
  const submit = async () => {
    /**
     * 1- 表单整体校验
     * 2- 校验通过执行绑定 => 获取登录人信息存储 => 跳转首页
     */
    const res = await fm.value.validate()
    try {
      if (res.valid) {
        // 校验通过
        const { result } = await userQQPatchLogin({
          unionId: props.unionId,
          ...formData
        })
        // 存储当前登录人信息
        await store.commit('user/setProfile', result)
        // 登录成功之后 => 合并之前本地购物车数据,到当前登录人名下
        await store.dispatch('cart/mergeCartAction')
        message({ type: 'success', text: '注册成功' })
        router.replace('/')
      }
    } catch (error) {
      message({ type: 'error', text: '注册失败' })
    }
  }
  return { submit }
}

export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  components: {
    Form,
    Field
  },
  setup (props) {
    const { form, rules } = useValidateForm()
    const { handlerCode, fm, count } = useSendCode(form, userQQPatchCode)
    const { submit } = useSubmit(fm, props, form)
    return { form, rules, handlerCode, fm, count, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
